package book.controller;

import book.entity.privileges;
import book.entity.role;
import book.entity.role_privileges;
import book.service.RolePrivilegesService;
import book.service.RoleServices;
import book.service.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleServices roleServices;

    @Autowired
    UserServices userServices;

    @Autowired
    RolePrivilegesService rolePrivilegesService;
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String Add(role id){

        return "role/addRole";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getAdd(role id){
        Map<String,Object> map=new HashMap<String,Object>();
        System.out.println(id.getName());
       roleServices.insertRole(id);
        map.put("result",true);
        return map;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") int id){
        Map<String, Object> map = new HashMap<String,Object>();
        rolePrivilegesService.deleterole(id);
        userServices.delrole(id);
        roleServices.delete(id);
        map.put("result",true);
        return map;
    }
}
